import edu.mju.bean.Role;
import edu.mju.mapper.RoleMapper;
import org.apache.ibatis.datasource.pooled.PooledDataSource;
import org.apache.ibatis.mapping.Environment;
import org.apache.ibatis.session.Configuration;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.apache.ibatis.transaction.TransactionFactory;
import org.apache.ibatis.transaction.jdbc.JdbcTransactionFactory;

/**
 * @Author: wyp
 * @Date: 2020/9/21 22:57
 */
public class MybatisConfig {
    public static SqlSessionFactory getSqlSessionFactory()  {
        //数据库连接池信息
        PooledDataSource dataSource = new PooledDataSource();
        dataSource.setDriver("com.mysql.jdbc.Driver");
        dataSource.setUrl("root");
        dataSource.setPassword("123456");
        dataSource.setUrl("jdbc:mysql://192.168.0.248:3306/chapter3?useUnicode=true&characterEncoding=UTF-8");
        //设置是否自动提交
        dataSource.setDefaultAutoCommit(false);

        //配置事务管理器
        TransactionFactory transactionFactory = new JdbcTransactionFactory();
        Environment environment = new Environment("development",transactionFactory,dataSource);

        //创建Configuration
        Configuration configuration = new Configuration(environment);
        //注册MyBatis别名
        configuration.getTypeAliasRegistry().registerAlias("role", Role.class);
        //注册映射器
        configuration.addMapper(RoleMapper.class);

        //构建SqlSessionFactory
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(configuration);

        return sqlSessionFactory;
    }
}
